﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Master/Site.master" %>

<%@ Import Namespace="System.IO" %>
 
<%@ Import Namespace="System.Web.Configuration" %>
 
<script runat="server">
    Private Const VirtualImageRoot As String = "~/"
    Private selectedRole As String, selectedUser As String
 
    Private Sub Page_Init()
        UserRoles.DataSource = Roles.GetAllRoles()
        UserRoles.DataBind()
 
        UserList.DataSource = Membership.GetAllUsers()
        UserList.DataBind()
         
        FolderTree.Nodes.Clear()
    End Sub
     
    Private Sub Page_Load()
        selectedRole = UserRoles.SelectedValue
        selectedUser = UserList.SelectedValue
    End Sub
 
    Private Sub Page_PreRender()
    End Sub
     
    Private Sub PopulateTree(byUserOrRole As String)
        '
        '		 * The PopulateTree and AddNodeAndDescendents are taken almost verbatim from Scott Mitchell's article
        '		 * "Using the TreeView Control and a DataList to Create an Online Image Gallery", which is located at 
        '		 * http://aspnet.4guysfromrolla.com/articles/083006-1.aspx
        '		 
 
  
        ' Populate the tree based on the subfolders of the specified VirtualImageRoot
        Dim rootFolder As New DirectoryInfo(Server.MapPath(VirtualImageRoot))
        Dim root As TreeNode = AddNodeAndDescendents(byUserOrRole, rootFolder, Nothing)
        FolderTree.Nodes.Add(root)
    End Sub

    Private Function AddNodeAndDescendents(byUserOrRole As String, folder As DirectoryInfo, parentNode As TreeNode) As TreeNode
        '
        '		 * The PopulateTree and AddNodeAndDescendents are taken almost verbatim from Scott Mitchell's article
        '		 * "Using the TreeView Control and a DataList to Create an Online Image Gallery", which is located at 
        '		 * http://aspnet.4guysfromrolla.com/articles/083006-1.aspx
        '		 
 
 
        ' Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value... 
        Dim virtualFolderPath As String
        If parentNode Is Nothing Then
            virtualFolderPath = VirtualImageRoot
        Else
            virtualFolderPath = Convert.ToString(parentNode.Value + folder.Name) & "/"
        End If
 
        ' Instantiate the objects that we'll use to check folder security on each tree node.
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath)
        Dim systemWeb As SystemWebSectionGroup = DirectCast(config.GetSectionGroup("system.web"), SystemWebSectionGroup)
        Dim section As AuthorizationSection = DirectCast(systemWeb.Sections("authorization"), AuthorizationSection)

        Dim action As String
        If byUserOrRole = "ByRole" Then
            action = GetTheRuleForThisRole(section, virtualFolderPath)
        ElseIf byUserOrRole = "ByUser" Then
            action = GetTheRuleForThisUser(section, virtualFolderPath)
        Else
            action = ""
        End If
 
        '  This is where I wanna adjust the folder name. 
        Dim node As New TreeNode(Convert.ToString(folder.Name) & " (" & action & ")", virtualFolderPath)
         
        
        If ((action.Substring(0, 5) = "ALLOW")) Then
            node.ImageUrl = "~/i/greenlight.gif"
        Else
            node.ImageUrl = "~/i/redlight.gif"
        End If
        node.NavigateUrl = "access_rules.aspx?selectedFolderName=" & Convert.ToString(folder.Name)

        ' Recurse through this folder's subfolders
        Dim subFolders As DirectoryInfo() = folder.GetDirectories()
        For Each subFolder As DirectoryInfo In subFolders
            ' You could use this filter out certain folders.
            If subFolder.Name <> "_controls" AndAlso subFolder.Name <> "App_Data" Then
                Dim child As TreeNode = AddNodeAndDescendents(byUserOrRole, subFolder, node)
                node.ChildNodes.Add(child)
            End If
        Next
        Return node
        ' Return the new TreeNode
    End Function
 

    Private Function GetTheRuleForThisRole(section As AuthorizationSection, folder As String) As String
        For Each rule As AuthorizationRule In section.Rules
            For Each user As String In rule.Users
                If user = "*" Then
                    Return rule.Action.ToString().ToUpper() & ": All Users"
                End If
            Next
            For Each role As String In rule.Roles
                If role = selectedRole Then
                    Return rule.Action.ToString().ToUpper() & ": Role=" & role
                End If
            Next
        Next
        Return "Allow"
    End Function

    Private Function GetTheRuleForThisUser(section As AuthorizationSection, folder As String) As String
        For Each rule As AuthorizationRule In section.Rules
            For Each user As String In rule.Users
                If user = "*" Then
                    Return rule.Action.ToString().ToUpper() & ": All Users"
                ElseIf user = selectedUser Then
                    Return rule.Action.ToString().ToUpper() & ": User=" & user
                End If
            Next

            ' Don't forget that users might belong to some roles!
            For Each role As String In rule.Roles
                If Roles.IsUserInRole(selectedUser, role) Then
                    Return rule.Action.ToString().ToUpper() & ": Role=" & role
                End If
            Next
        Next
        Return "ALLOW"
    End Function

    Private Sub DisplayRoleSummary(sender As Object, e As EventArgs)
        FolderTree.Nodes.Clear()
        UserList.SelectedIndex = 0
        If UserRoles.SelectedIndex > 0 Then
            PopulateTree("ByRole")
            FolderTree.ExpandAll()
        End If
    End Sub
 
    Private Sub DisplayUserSummary(sender As Object, e As EventArgs)
        FolderTree.Nodes.Clear()
        UserRoles.SelectedIndex = 0
        If UserList.SelectedIndex > 0 Then
            PopulateTree("ByUser")
            FolderTree.ExpandAll()
        End If
    End Sub
  
    Private Sub DisplaySecuritySummary(sender As Object, e As TreeNodeEventArgs)
        e.Node.ShowCheckBox = True
    End Sub
 
    Protected Sub FolderTree_SelectedNodeChanged(sender As Object, e As EventArgs)
    End Sub
</script> 

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- #include file="_nav.aspx -->
    <table class="webparts">
<tr>
	<th>Website Access Security Summary</th>
</tr>
<tr>
	<td class="details" valign="top">
		<table>
		<tr>
			<td valign="top" style="padding-right: 30px;">
				
				<asp:DropDownList ID="UserRoles" runat="server" AppendDataBoundItems="true"
					AutoPostBack="true" OnSelectedIndexChanged="DisplayRoleSummary">
				<asp:ListItem>Select Role</asp:ListItem>
				</asp:DropDownList>
				
				&nbsp;&nbsp;&nbsp;&nbsp;<b>&mdash;&nbsp;&nbsp;OR&nbsp;&nbsp;&mdash;</b>
				&nbsp;&nbsp;&nbsp;				
				
				<asp:DropDownList ID="UserList" runat="server" AppendDataBoundItems="true"
					AutoPostBack="true" OnSelectedIndexChanged="DisplayUserSummary">
				<asp:ListItem>Select User</asp:ListItem>
				<asp:ListItem Text="Anonymous users (?)" Value="?"></asp:ListItem>
				<asp:ListItem Text="Authenticated users not in a role (*)" Value="*"></asp:ListItem>
				</asp:DropDownList>	
				
				<br />
				
				<div class="treeview">
				<asp:TreeView runat="server" ID="FolderTree"
					OnSelectedNodeChanged="FolderTree_SelectedNodeChanged"
					>
					<RootNodeStyle ImageUrl="~/i/folder.gif" />
					<ParentNodeStyle ImageUrl="~/i/folder.gif" />
					<LeafNodeStyle ImageUrl="~/i/folder.gif" />
					<SelectedNodeStyle Font-Underline="true" ForeColor="#A21818" />
				</asp:TreeView>
				</div>
			</td>
		</tr>
		</table>
	</td>
</tr>
</table>
</asp:Content>

